<?php

require_once('classes/tickets.php');


class resultHandler{

	private $ticketsObj;
	private $allTickets = array();
	private $resultsOnPtotalPrice = 10;
	private $displayCustom = true;
	private $originalTickets = array();
	private $totalRows;
	private $currentPage;
        
	
	public function resultHandler($searchQ){
		$this->ticketsObj = new Tickets($searchQ);
		$this->allTickets  =  $this->ticketsObj->getAllTickets();
		$this->quickSort($this->allTickets);
	}

	public function getAllTickets(){
		return $this->allTickets;
	}
	
	private function originalTickets(){
		return $this->originalTickets;
	}
	
	public function setTickets($tickets){
		$this->allTickets = $tickets;
	}
	
	public function displayResults($page){
                $this->currentPage = $page;
		$from = $page * $this->resultsOnPtotalPrice - $this->resultsOnPtotalPrice;
		$to = $from + $this->resultsOnPtotalPrice;
		
		$pageResults = array();

		$x = $from;
		$totalRows = 0;
		
		$x = 0;
		for($i = 0; $i < count($this->allTickets); $i++){
			if((count(array_unique($this->allTickets[$i]->filter)) === 1) && ($i >= $from)) {
			//	echo $from . '--' . $to; 
				if($x < $this->resultsOnPtotalPrice){
					array_push($pageResults,$this->allTickets[$i]);
					$x++;
				}
			}
			if(count(array_unique($this->allTickets[$i]->filter)) === 1){
				$totalRows++;
			}
		}
		$this->totalRows = $totalRows;
		return $pageResults;
	}
	
	public function getTotalRows(){
		return $this->totalRows;
	}
        
        public function getCurrentPage(){
            return $this->currentPage;
        }
	
	public function getTotalRowsOnPage(){
		return $this->resultsOnPtotalPrice;
	}
	// Non-recurive Quicksort for an array of Person objects
	// adapted from http://www.algorithmist.com/index.php/Quicksort_non-recursive.php
	private function quickSort( &$array ){
		 $cur = 1;
		 $stack[1]['l'] = 0;
		 $stack[1]['r'] = count($array)-1;

		 do
		 {
		  $l = $stack[$cur]['l'];
		  $r = $stack[$cur]['r'];
		  $cur--;

		  do
		  {
		   $i = $l;
		   $j = $r;
		   $tmp = $array[(int)( ($l+$r)/2 )];

		   // partion the array in two parts.
		   // left from $tmp are with smaller values,
		   // right from $tmp are with bigger ones
		   do
		   {
			while( $array[$i]->totalPrice < $tmp->totalPrice )
			 $i++;

			while( $tmp->totalPrice < $array[$j]->totalPrice )
			 $j--;

			// swap elements from the two sides
			if( $i <= $j)
			{
			 $w = $array[$i];
			 $array[$i] = $array[$j];
			 $array[$j] = $w;

			 $i++;
			 $j--;
			}

		   }while( $i <= $j );

		 if( $i < $r )
		   {
			$cur++;
			$stack[$cur]['l'] = $i;
			$stack[$cur]['r'] = $r;
		   }
		   $r = $j;

		  }while( $l < $r );

		 }while( $cur != 0 );
	}

}

?>